package com.amazon.jobtest.test;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.junit.Test;

import com.amazon.jobtest.Student;
import com.amazon.jobtest.TestResult;
import com.amazon.jobtest.util.DateUtil;

public class ExamTestCases {
/*
	@Test
	public void test() {
		
		fail("Not yet implemented");
	}
*/
	@Test
	public void addTestResult(){
		Student student = new Student();
		if(!student.addTestResult(new TestResult(new Date(), 3))){
			fail("Problem on add Test Result");
		}
		if(!isOrdened(student.getTestResultList())){
			fail("The item inclusion disordened de list");
		}
		if(!isOrdened(student.getTestResultList())){
			fail("The item inclusion disordened de list");
		}
		if(!isOrdened(student.getTestResultList())){
			fail("The item inclusion disordened de list");
		}
		if(!isOrdened(student.getTestResultList())){
			fail("The item inclusion disordened de list");
		}
		if(!isOrdened(student.getTestResultList())){
			fail("The item inclusion disordened de list");
		}
	}

	@Test
	public void testStudentAvarage() throws Exception{
		Student student = new Student();
		TestResult test = null;
		float avarage = 0;
		
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		
		avarage = student.getAverageTestResult();
		if(avarage != 6){
			fail("The avarage is diferent than" + 6);
		}
		
		student = new Student();
		
		test = new TestResult(new Date(), 1);
		student.addTestResult(test);

		test = new TestResult(new Date(), 2);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 3);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 4);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 5);
		student.addTestResult(test);

		
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		
		
		test = new TestResult(new Date(), 7);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 8);
		student.addTestResult(test);
		
		
		test = new TestResult(new Date(), 9);
		student.addTestResult(test);
		
		
		avarage = student.getAverageTestResult();
		if(avarage != 7){
			fail("The avarage is diferent than" + 7);
		}
	}
	
	@Test
	public void testStudentAvarageWithoutEnoughtFilds() throws Exception{
		Student student = new Student();
		TestResult test = null;
		float avarage = 0;
		int exceptions = 0;
		try{
		avarage = student.getAverageTestResult();
		} catch (Exception e){
			exceptions++;
			System.out.println(e.getMessage());
		}
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		try{
		exceptions++;	
		avarage = student.getAverageTestResult();
		}catch(Exception e) {
			System.out.println(e.getMessage());
		}
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		
		test = new TestResult(new Date(), 6);
		student.addTestResult(test);
		avarage = student.getAverageTestResult();
		if(avarage != 6){
			fail("The avarage is diferent than" + 6);
		}
		if(exceptions != 2){
			fail("There shuld be happenned 2 exceptions and happened: " + exceptions);
		}
	}
	
	private boolean isOrdened(List<TestResult> testResultList) {
		Iterator<TestResult> iterator = testResultList.iterator();
		TestResult result = null;
		TestResult nextTestResult = null;
		if(iterator.hasNext())
			result = iterator.next();
		while(iterator.hasNext()){
			nextTestResult = iterator.next();
			//the result must be bigger than the nextResult for the list be considered ordened
			if(result.getScore() > nextTestResult.getScore()){
				return false;
			}
			result = nextTestResult;
		}
		return true;
	}
	@Test
	public void testDateFormater(){
		if(DateUtil.stringToDate("1999-01-07", "yyyy-MM-dd") == null){
			fail("The stringToDate was unable to convet a String");
		}

		
	}
}
